﻿
@{
    ViewBag.Title = "CreateTask";
    Layout = "~/Views/Shared/SiteLayout.cshtml";
}
<script type="text/javascript" src="@Url.Content("/Scripts/knockout-2.2.1.js")"></script>
<script type="text/javascript">
    $(document).ready(function() {

        function getViewModel() {
            //Make the self as 'this' reference
            var self = this;
            //Declare observable which will be bind with UI 
            $.ajax({
                type: "POST",
                url: "/Task/GetViewModel",
                contentType: "application/json",
                dataType: "json",
                async: false,
                success: function(data) {
                    self.iterationList = data.Iterations;
                    self.assignedList = data.Users;
                    self.stateList = data.States;
                    self.blockList = data.Blocks;
                    self.areaList = data.Areas;
                    self.activityList = data.Activities;

                    self.assignedSelected = ko.observable(data.Users[0].UserId);
                    self.iterationSelected = ko.observable(data.Iterations[0].Id);
                    self.stateSelected = ko.observable(data.States[0].Id);
                    self.blockSelected = ko.observable(data.Blocks[0].Id);
                    self.areaSelected = ko.observable(data.Areas[0].Id);

                    self.title = ko.observable();
                    self.remainingWork = ko.observable();
                    self.priority = ko.observable();
                    self.description = ko.observable();
                }
            });

            self.save = function() {

                var task = {
                    Title: self.title(),
                    EstimatedOrEffort: self.remainingWork(),
                    Priority: self.priority(),
                    Iteration: self.iterationSelected(),
                    AssignedTo: self.assignedSelected(),
                    Status: self.stateSelected(),
                    Block: self.blockSelected(),
                    Description: $('#txtaDescription').val() == '' ? null : $('#txtaDescription').val(),
                    Area: self.areaSelected(),
                };
                // TO DO: Code here to save data to db.
                $.ajax({
                    type: "POST",
                    url: "/Task/CreateTask",
                    data: ko.toJSON(task),
                    contentType: "application/json",
                    success: function(data) {
                        alert(data.toString());
                    },
                    error: function() {
                        alert("Fail");
                    }
                });
            };
            return self;
        }
        var viewModel = getViewModel();
        ko.applyBindings(viewModel);
    });
        </script>
<div>
        @Html.Partial("TaskManagerLayout")
</div>
